home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_emacs.idb / usr / freeware / info / cl-5.z / cl-5 (.txt)
GNU Info File  |  1998-10-27  |  44KB  |  749 lines

  1. This is Info file ../info/cl, produced by Makeinfo-1.64 from the input
  2. file cl.texi.
  3.    This file documents the GNU Emacs Common Lisp emulation package.
  4.    Copyright (C) 1993 Free Software Foundation, Inc.
  5.    Permission is granted to make and distribute verbatim copies of this
  6. manual provided the copyright notice and this permission notice are
  7. preserved on all copies.
  8.    Permission is granted to copy and distribute modified versions of
  9. this manual under the conditions for verbatim copying, provided also
  10. that the section entitled "GNU General Public License" is included
  11. exactly as in the original, and provided that the entire resulting
  12. derived work is distributed under the terms of a permission notice
  13. identical to this one.
  14.    Permission is granted to copy and distribute translations of this
  15. manual into another language, under the above conditions for modified
  16. versions, except that the section entitled "GNU General Public License"
  17. may be included in a translation approved by the author instead of in
  18. the original English.
  19. File: cl,  Node: Structures,  Next: Assertions,  Prev: Hash Tables,  Up: Top
  20. Structures
  21. **********
  22. The Common Lisp "structure" mechanism provides a general way to define
  23. data types similar to C's `struct' types.  A structure is a Lisp object
  24. containing some number of "slots", each of which can hold any Lisp data
  25. object.  Functions are provided for accessing and setting the slots,
  26. creating or copying structure objects, and recognizing objects of a
  27. particular structure type.
  28.    In true Common Lisp, each structure type is a new type distinct from
  29. all existing Lisp types.  Since the underlying Emacs Lisp system
  30. provides no way to create new distinct types, this package implements
  31. structures as vectors (or lists upon request) with a special "tag"
  32. symbol to identify them.
  33.  - Special Form: defstruct NAME SLOTS...
  34.      The `defstruct' form defines a new structure type called NAME,
  35.      with the specified SLOTS.  (The SLOTS may begin with a string
  36.      which documents the structure type.) In the simplest case, NAME
  37.      and each of the SLOTS are symbols.  For example,
  38.           (defstruct person name age sex)
  39.      defines a struct type called `person' which contains three slots.
  40.      Given a `person' object P, you can access those slots by calling
  41.      `(person-name P)', `(person-age P)', and `(person-sex P)'.  You
  42.      can also change these slots by using `setf' on any of these place
  43.      forms:
  44.           (incf (person-age birthday-boy))
  45.      You can create a new `person' by calling `make-person', which
  46.      takes keyword arguments `:name', `:age', and `:sex' to specify the
  47.      initial values of these slots in the new object.  (Omitting any of
  48.      these arguments leaves the corresponding slot "undefined,"
  49.      according to the Common Lisp standard; in Emacs Lisp, such
  50.      uninitialized slots are filled with `nil'.)
  51.      Given a `person', `(copy-person P)' makes a new object of the same
  52.      type whose slots are `eq' to those of P.
  53.      Given any Lisp object X, `(person-p X)' returns true if X looks
  54.      like a `person', false otherwise.  (Again, in Common Lisp this
  55.      predicate would be exact; in Emacs Lisp the best it can do is
  56.      verify that X is a vector of the correct length which starts with
  57.      the correct tag symbol.)
  58.      Accessors like `person-name' normally check their arguments
  59.      (effectively using `person-p') and signal an error if the argument
  60.      is the wrong type.  This check is affected by `(optimize (safety
  61.      ...))' declarations.  Safety level 1, the default, uses a somewhat
  62.      optimized check that will detect all incorrect arguments, but may
  63.      use an uninformative error message (e.g., "expected a vector"
  64.      instead of "expected a `person'").  Safety level 0 omits all
  65.      checks except as provided by the underlying `aref' call; safety
  66.      levels 2 and 3 do rigorous checking that will always print a
  67.      descriptive error message for incorrect inputs.  *Note
  68.      Declarations::.
  69.           (setq dave (make-person :name "Dave" :sex 'male))
  70.                => [cl-struct-person "Dave" nil male]
  71.           (setq other (copy-person dave))
  72.                => [cl-struct-person "Dave" nil male]
  73.           (eq dave other)
  74.                => nil
  75.           (eq (person-name dave) (person-name other))
  76.                => t
  77.           (person-p dave)
  78.                => t
  79.           (person-p [1 2 3 4])
  80.                => nil
  81.           (person-p "Bogus")
  82.                => nil
  83.           (person-p '[cl-struct-person counterfeit person object])
  84.                => t
  85.      In general, NAME is either a name symbol or a list of a name
  86.      symbol followed by any number of "struct options"; each SLOT is
  87.      either a slot symbol or a list of the form `(SLOT-NAME
  88.      DEFAULT-VALUE SLOT-OPTIONS...)'.  The DEFAULT-VALUE is a Lisp form
  89.      which is evaluated any time an instance of the structure type is
  90.      created without specifying that slot's value.
  91.      Common Lisp defines several slot options, but the only one
  92.      implemented in this package is `:read-only'.  A non-`nil' value
  93.      for this option means the slot should not be `setf'-able; the
  94.      slot's value is determined when the object is created and does not
  95.      change afterward.
  96.           (defstruct person
  97.             (name nil :read-only t)
  98.             age
  99.             (sex 'unknown))
  100.      Any slot options other than `:read-only' are ignored.
  101.      For obscure historical reasons, structure options take a different
  102.      form than slot options.  A structure option is either a keyword
  103.      symbol, or a list beginning with a keyword symbol possibly followed
  104.      by arguments.  (By contrast, slot options are key-value pairs not
  105.      enclosed in lists.)
  106.           (defstruct (person (:constructor create-person)
  107.                              (:type list)
  108.                              :named)
  109.             name age sex)
  110.      The following structure options are recognized.
  111.     `:conc-name'
  112.           The argument is a symbol whose print name is used as the
  113.           prefix for the names of slot accessor functions.  The default
  114.           is the name of the struct type followed by a hyphen.  The
  115.           option `(:conc-name p-)' would change this prefix to `p-'.
  116.           Specifying `nil' as an argument means no prefix, so that the
  117.           slot names themselves are used to name the accessor functions.
  118.     `:constructor'
  119.           In the simple case, this option takes one argument which is an
  120.           alternate name to use for the constructor function.  The
  121.           default is `make-NAME', e.g., `make-person'.  The above
  122.           example changes this to `create-person'.  Specifying `nil' as
  123.           an argument means that no standard constructor should be
  124.           generated at all.
  125.           In the full form of this option, the constructor name is
  126.           followed by an arbitrary argument list.  *Note Program
  127.           Structure::, for a description of the format of Common Lisp
  128.           argument lists.  All options, such as `&rest' and `&key', are
  129.           supported.  The argument names should match the slot names;
  130.           each slot is initialized from the corresponding argument.
  131.           Slots whose names do not appear in the argument list are
  132.           initialized based on the DEFAULT-VALUE in their slot
  133.           descriptor.  Also, `&optional' and `&key' arguments which
  134.           don't specify defaults take their defaults from the slot
  135.           descriptor.  It is legal to include arguments which don't
  136.           correspond to slot names; these are useful if they are
  137.           referred to in the defaults for optional, keyword, or `&aux'
  138.           arguments which *do* correspond to slots.
  139.           You can specify any number of full-format `:constructor'
  140.           options on a structure.  The default constructor is still
  141.           generated as well unless you disable it with a simple-format
  142.           `:constructor' option.
  143.                (defstruct
  144.                 (person
  145.                  (:constructor nil)   ; no default constructor
  146.                  (:constructor new-person (name sex &optional (age 0)))
  147.                  (:constructor new-hound (&key (name "Rover")
  148.                                                (dog-years 0)
  149.                                           &aux (age (* 7 dog-years))
  150.                                                (sex 'canine))))
  151.                 name age sex)
  152.           The first constructor here takes its arguments positionally
  153.           rather than by keyword.  (In official Common Lisp
  154.           terminology, constructors that work By Order of Arguments
  155.           instead of by keyword are called "BOA constructors."  No, I'm
  156.           not making this up.)  For example, `(new-person "Jane"
  157.           'female)' generates a person whose slots are `"Jane"', 0, and
  158.           `female', respectively.
  159.           The second constructor takes two keyword arguments, `:name',
  160.           which initializes the `name' slot and defaults to `"Rover"',
  161.           and `:dog-years', which does not itself correspond to a slot
  162.           but which is used to initialize the `age' slot.  The `sex'
  163.           slot is forced to the symbol `canine' with no syntax for
  164.           overriding it.
  165.     `:copier'
  166.           The argument is an alternate name for the copier function for
  167.           this type.  The default is `copy-NAME'.  `nil' means not to
  168.           generate a copier function.  (In this implementation, all
  169.           copier functions are simply synonyms for `copy-sequence'.)
  170.     `:predicate'
  171.           The argument is an alternate name for the predicate which
  172.           recognizes objects of this type.  The default is `NAME-p'.
  173.           `nil' means not to generate a predicate function.  (If the
  174.           `:type' option is used without the `:named' option, no
  175.           predicate is ever generated.)
  176.           In true Common Lisp, `typep' is always able to recognize a
  177.           structure object even if `:predicate' was used.  In this
  178.           package, `typep' simply looks for a function called
  179.           `TYPENAME-p', so it will work for structure types only if
  180.           they used the default predicate name.
  181.     `:include'
  182.           This option implements a very limited form of C++-style
  183.           inheritance.  The argument is the name of another structure
  184.           type previously created with `defstruct'.  The effect is to
  185.           cause the new structure type to inherit all of the included
  186.           structure's slots (plus, of course, any new slots described
  187.           by this struct's slot descriptors).  The new structure is
  188.           considered a "specialization" of the included one.  In fact,
  189.           the predicate and slot accessors for the included type will
  190.           also accept objects of the new type.
  191.           If there are extra arguments to the `:include' option after
  192.           the included-structure name, these options are treated as
  193.           replacement slot descriptors for slots in the included
  194.           structure, possibly with modified default values.  Borrowing
  195.           an example from Steele:
  196.                (defstruct person name (age 0) sex)
  197.                     => person
  198.                (defstruct (astronaut (:include person (age 45)))
  199.                  helmet-size
  200.                  (favorite-beverage 'tang))
  201.                     => astronaut
  202.                
  203.                (setq joe (make-person :name "Joe"))
  204.                     => [cl-struct-person "Joe" 0 nil]
  205.                (setq buzz (make-astronaut :name "Buzz"))
  206.                     => [cl-struct-astronaut "Buzz" 45 nil nil tang]
  207.                
  208.                (list (person-p joe) (person-p buzz))
  209.                     => (t t)
  210.                (list (astronaut-p joe) (astronaut-p buzz))
  211.                     => (nil t)
  212.                
  213.                (person-name buzz)
  214.                     => "Buzz"
  215.                (astronaut-name joe)
  216.                     => error: "astronaut-name accessing a non-astronaut"
  217.           Thus, if `astronaut' is a specialization of `person', then
  218.           every `astronaut' is also a `person' (but not the other way
  219.           around).  Every `astronaut' includes all the slots of a
  220.           `person', plus extra slots that are specific to astronauts.
  221.           Operations that work on people (like `person-name') work on
  222.           astronauts just like other people.
  223.     `:print-function'
  224.           In full Common Lisp, this option allows you to specify a
  225.           function which is called to print an instance of the
  226.           structure type.  The Emacs Lisp system offers no hooks into
  227.           the Lisp printer which would allow for such a feature, so
  228.           this package simply ignores `:print-function'.
  229.     `:type'
  230.           The argument should be one of the symbols `vector' or `list'.
  231.           This tells which underlying Lisp data type should be used to
  232.           implement the new structure type.  Vectors are used by
  233.           default, but `(:type list)' will cause structure objects to
  234.           be stored as lists instead.
  235.           The vector representation for structure objects has the
  236.           advantage that all structure slots can be accessed quickly,
  237.           although creating vectors is a bit slower in Emacs Lisp.
  238.           Lists are easier to create, but take a relatively long time
  239.           accessing the later slots.
  240.     `:named'
  241.           This option, which takes no arguments, causes a
  242.           characteristic "tag" symbol to be stored at the front of the
  243.           structure object.  Using `:type' without also using `:named'
  244.           will result in a structure type stored as plain vectors or
  245.           lists with no identifying features.
  246.           The default, if you don't specify `:type' explicitly, is to
  247.           use named vectors.  Therefore, `:named' is only useful in
  248.           conjunction with `:type'.
  249.                (defstruct (person1) name age sex)
  250.                (defstruct (person2 (:type list) :named) name age sex)
  251.                (defstruct (person3 (:type list)) name age sex)
  252.                
  253.                (setq p1 (make-person1))
  254.                     => [cl-struct-person1 nil nil nil]
  255.                (setq p2 (make-person2))
  256.                     => (person2 nil nil nil)
  257.                (setq p3 (make-person3))
  258.                     => (nil nil nil)
  259.                
  260.                (person1-p p1)
  261.                     => t
  262.                (person2-p p2)
  263.                     => t
  264.                (person3-p p3)
  265.                     => error: function person3-p undefined
  266.           Since unnamed structures don't have tags, `defstruct' is not
  267.           able to make a useful predicate for recognizing them.  Also,
  268.           accessors like `person3-name' will be generated but they will
  269.           not be able to do any type checking.  The `person3-name'
  270.           function, for example, will simply be a synonym for `car' in
  271.           this case.  By contrast, `person2-name' is able to verify
  272.           that its argument is indeed a `person2' object before
  273.           proceeding.
  274.     `:initial-offset'
  275.           The argument must be a nonnegative integer.  It specifies a
  276.           number of slots to be left "empty" at the front of the
  277.           structure.  If the structure is named, the tag appears at the
  278.           specified position in the list or vector; otherwise, the first
  279.           slot appears at that position.  Earlier positions are filled
  280.           with `nil' by the constructors and ignored otherwise.  If the
  281.           type `:include's another type, then `:initial-offset'
  282.           specifies a number of slots to be skipped between the last
  283.           slot of the included type and the first new slot.
  284.    Except as noted, the `defstruct' facility of this package is
  285. entirely compatible with that of Common Lisp.
  286. File: cl,  Node: Assertions,  Next: Efficiency Concerns,  Prev: Structures,  Up: Top
  287. Assertions and Errors
  288. *********************
  289. This section describes two macros that test "assertions", i.e.,
  290. conditions which must be true if the program is operating correctly.
  291. Assertions never add to the behavior of a Lisp program; they simply
  292. make "sanity checks" to make sure everything is as it should be.
  293.    If the optimization property `speed' has been set to 3, and `safety'
  294. is less than 3, then the byte-compiler will optimize away the following
  295. assertions.  Because assertions might be optimized away, it is a bad
  296. idea for them to include side-effects.
  297.  - Special Form: assert TEST-FORM [SHOW-ARGS STRING ARGS...]
  298.      This form verifies that TEST-FORM is true (i.e., evaluates to a
  299.      non-`nil' value).  If so, it returns `nil'.  If the test is not
  300.      satisfied, `assert' signals an error.
  301.      A default error message will be supplied which includes TEST-FORM.
  302.      You can specify a different error message by including a STRING
  303.      argument plus optional extra arguments.  Those arguments are simply
  304.      passed to `error' to signal the error.
  305.      If the optional second argument SHOW-ARGS is `t' instead of `nil',
  306.      then the error message (with or without STRING) will also include
  307.      all non-constant arguments of the top-level FORM.  For example:
  308.           (assert (> x 10) t "x is too small: %d")
  309.      This usage of SHOW-ARGS is an extension to Common Lisp.  In true
  310.      Common Lisp, the second argument gives a list of PLACES which can
  311.      be `setf''d by the user before continuing from the error.  Since
  312.      Emacs Lisp does not support continuable errors, it makes no sense
  313.      to specify PLACES.
  314.  - Special Form: check-type FORM TYPE [STRING]
  315.      This form verifies that FORM evaluates to a value of type TYPE.
  316.      If so, it returns `nil'.  If not, `check-type' signals a
  317.      `wrong-type-argument' error.  The default error message lists the
  318.      erroneous value along with TYPE and FORM themselves.  If STRING is
  319.      specified, it is included in the error message in place of TYPE.
  320.      For example:
  321.           (check-type x (integer 1 *) "a positive integer")
  322.      *Note Type Predicates::, for a description of the type specifiers
  323.      that may be used for TYPE.
  324.      Note that in Common Lisp, the first argument to `check-type' must
  325.      be a PLACE suitable for use by `setf', because `check-type'
  326.      signals a continuable error that allows the user to modify PLACE.
  327.    The following error-related macro is also defined:
  328.  - Special Form: ignore-errors FORMS...
  329.      This executes FORMS exactly like a `progn', except that errors are
  330.      ignored during the FORMS.  More precisely, if an error is signaled
  331.      then `ignore-errors' immediately aborts execution of the FORMS and
  332.      returns `nil'.  If the FORMS complete successfully, `ignore-errors'
  333.      returns the result of the last FORM.
  334. File: cl,  Node: Efficiency Concerns,  Next: Common Lisp Compatibility,  Prev: Assertions,  Up: Top
  335. Efficiency Concerns
  336. *******************
  337. Macros
  338. ======
  339. Many of the advanced features of this package, such as `defun*',
  340. `loop', and `setf', are implemented as Lisp macros.  In byte-compiled
  341. code, these complex notations will be expanded into equivalent Lisp
  342. code which is simple and efficient.  For example, the forms
  343.      (incf i n)
  344.      (push x (car p))
  345. are expanded at compile-time to the Lisp forms
  346.      (setq i (+ i n))
  347.      (setcar p (cons x (car p)))
  348. which are the most efficient ways of doing these respective operations
  349. in Lisp.  Thus, there is no performance penalty for using the more
  350. readable `incf' and `push' forms in your compiled code.
  351.    *Interpreted* code, on the other hand, must expand these macros
  352. every time they are executed.  For this reason it is strongly
  353. recommended that code making heavy use of macros be compiled.  (The
  354. features labeled "Special Form" instead of "Function" in this manual
  355. are macros.)  A loop using `incf' a hundred times will execute
  356. considerably faster if compiled, and will also garbage-collect less
  357. because the macro expansion will not have to be generated, used, and
  358. thrown away a hundred times.
  359.    You can find out how a macro expands by using the `cl-prettyexpand'
  360. function.
  361.  - Function: cl-prettyexpand FORM &optional FULL
  362.      This function takes a single Lisp form as an argument and inserts
  363.      a nicely formatted copy of it in the current buffer (which must be
  364.      in Lisp mode so that indentation works properly).  It also expands
  365.      all Lisp macros which appear in the form.  The easiest way to use
  366.      this function is to go to the `*scratch*' buffer and type, say,
  367.           (cl-prettyexpand '(loop for x below 10 collect x))
  368.      and type `C-x C-e' immediately after the closing parenthesis; the
  369.      expansion
  370.           (block nil
  371.             (let* ((x 0)
  372.                    (G1004 nil))
  373.               (while (< x 10)
  374.                 (setq G1004 (cons x G1004))
  375.                 (setq x (+ x 1)))
  376.               (nreverse G1004)))
  377.      will be inserted into the buffer.  (The `block' macro is expanded
  378.      differently in the interpreter and compiler, so `cl-prettyexpand'
  379.      just leaves it alone.  The temporary variable `G1004' was created
  380.      by `gensym'.)
  381.      If the optional argument FULL is true, then *all* macros are
  382.      expanded, including `block', `eval-when', and compiler macros.
  383.      Expansion is done as if FORM were a top-level form in a file being
  384.      compiled.  For example,
  385.           (cl-prettyexpand '(pushnew 'x list))
  386.                -| (setq list (adjoin 'x list))
  387.           (cl-prettyexpand '(pushnew 'x list) t)
  388.                -| (setq list (if (memq 'x list) list (cons 'x list)))
  389.           (cl-prettyexpand '(caddr (member* 'a list)) t)
  390.                -| (car (cdr (cdr (memq 'a list))))
  391.      Note that `adjoin', `caddr', and `member*' all have built-in
  392.      compiler macros to optimize them in common cases.
  393. Error Checking
  394. ==============
  395. Common Lisp compliance has in general not been sacrificed for the sake
  396. of efficiency.  A few exceptions have been made for cases where
  397. substantial gains were possible at the expense of marginal
  398. incompatibility.  One example is the use of `memq' (which is treated
  399. very efficiently by the byte-compiler) to scan for keyword arguments;
  400. this can become confused in rare cases when keyword symbols are used as
  401. both keywords and data values at once.  This is extremely unlikely to
  402. occur in practical code, and the use of `memq' allows functions with
  403. keyword arguments to be nearly as fast as functions that use
  404. `&optional' arguments.
  405.    The Common Lisp standard (as embodied in Steele's book) uses the
  406. phrase "it is an error if" to indicate a situation which is not
  407. supposed to arise in complying programs; implementations are strongly
  408. encouraged but not required to signal an error in these situations.
  409. This package sometimes omits such error checking in the interest of
  410. compactness and efficiency.  For example, `do' variable specifiers are
  411. supposed to be lists of one, two, or three forms; extra forms are
  412. ignored by this package rather than signaling a syntax error.  The
  413. `endp' function is simply a synonym for `null' in this package.
  414. Functions taking keyword arguments will accept an odd number of
  415. arguments, treating the trailing keyword as if it were followed by the
  416. value `nil'.
  417.    Argument lists (as processed by `defun*' and friends) *are* checked
  418. rigorously except for the minor point just mentioned; in particular,
  419. keyword arguments are checked for validity, and `&allow-other-keys' and
  420. `:allow-other-keys' are fully implemented.  Keyword validity checking
  421. is slightly time consuming (though not too bad in byte-compiled code);
  422. you can use `&allow-other-keys' to omit this check.  Functions defined
  423. in this package such as `find' and `member*' do check their keyword
  424. arguments for validity.
  425. Optimizing Compiler
  426. ===================
  427. The byte-compiler that comes with Emacs 18 normally fails to expand
  428. macros that appear in top-level positions in the file (i.e., outside of
  429. `defun's or other enclosing forms).  This would have disastrous
  430. consequences to programs that used such top-level macros as `defun*',
  431. `eval-when', and `defstruct'.  To work around this problem, the "CL"
  432. package patches the Emacs 18 compiler to expand top-level macros.  This
  433. patch will apply to your own macros, too, if they are used in a
  434. top-level context.  The patch will not harm versions of the Emacs 18
  435. compiler which have already had a similar patch applied, nor will it
  436. affect the optimizing Emacs 19 byte-compiler written by Jamie Zawinski
  437. and Hallvard Furuseth.  The patch is applied to the byte compiler's
  438. code in Emacs' memory, *not* to the `bytecomp.elc' file stored on disk.
  439.    The Emacs 19 compiler (for Emacs 18) is available from various Emacs
  440. Lisp archive sites such as `archive.cis.ohio-state.edu'.  Its use is
  441. highly recommended; many of the Common Lisp macros emit code which can
  442. be improved by optimization.  In particular, `block's (whether explicit
  443. or implicit in constructs like `defun*' and `loop') carry a fair
  444. run-time penalty; the optimizing compiler removes `block's which are
  445. not actually referenced by `return' or `return-from' inside the block.
  446. File: cl,  Node: Common Lisp Compatibility,  Next: Old CL Compatibility,  Prev: Efficiency Concerns,  Up: Top
  447. Common Lisp Compatibility
  448. *************************
  449. Following is a list of all known incompatibilities between this package
  450. and Common Lisp as documented in Steele (2nd edition).
  451.    Certain function names, such as `member', `assoc', and `floor', were
  452. already taken by (incompatible) Emacs Lisp functions; this package
  453. appends `*' to the names of its Common Lisp versions of these functions.
  454.    The word `defun*' is required instead of `defun' in order to use
  455. extended Common Lisp argument lists in a function.  Likewise,
  456. `defmacro*' and `function*' are versions of those forms which
  457. understand full-featured argument lists.  The `&whole' keyword does not
  458. work in `defmacro' argument lists (except inside recursive argument
  459. lists).
  460.    In order to allow an efficient implementation, keyword arguments use
  461. a slightly cheesy parser which may be confused if a keyword symbol is
  462. passed as the *value* of another keyword argument.  (Specifically,
  463. `(memq :KEYWORD REST-OF-ARGUMENTS)' is used to scan for `:KEYWORD'
  464. among the supplied keyword arguments.)
  465.    The `eql' and `equal' predicates do not distinguish between IEEE
  466. floating-point plus and minus zero.  The `equalp' predicate has several
  467. differences with Common Lisp; *note Predicates::..
  468.    The `setf' mechanism is entirely compatible, except that
  469. setf-methods return a list of five values rather than five values
  470. directly.  Also, the new "`setf' function" concept (typified by `(defun
  471. (setf foo) ...)') is not implemented.
  472.    The `do-all-symbols' form is the same as `do-symbols' with no
  473. OBARRAY argument.  In Common Lisp, this form would iterate over all
  474. symbols in all packages.  Since Emacs obarrays are not a first-class
  475. package mechanism, there is no way for `do-all-symbols' to locate any
  476. but the default obarray.
  477.    The `loop' macro is complete except that `loop-finish' and type
  478. specifiers are unimplemented.
  479.    The multiple-value return facility treats lists as multiple values,
  480. since Emacs Lisp cannot support multiple return values directly.  The
  481. macros will be compatible with Common Lisp if `values' or `values-list'
  482. is always used to return to a `multiple-value-bind' or other
  483. multiple-value receiver; if `values' is used without
  484. `multiple-value-...' or vice-versa the effect will be different from
  485. Common Lisp.
  486.    Many Common Lisp declarations are ignored, and others match the
  487. Common Lisp standard in concept but not in detail.  For example, local
  488. `special' declarations, which are purely advisory in Emacs Lisp, do not
  489. rigorously obey the scoping rules set down in Steele's book.
  490.    The variable `*gensym-counter*' starts out with a pseudo-random
  491. value rather than with zero.  This is to cope with the fact that
  492. generated symbols become interned when they are written to and loaded
  493. back from a file.
  494.    The `defstruct' facility is compatible, except that structures are
  495. of type `:type vector :named' by default rather than some special,
  496. distinct type.  Also, the `:type' slot option is ignored.
  497.    The second argument of `check-type' is treated differently.
  498. File: cl,  Node: Old CL Compatibility,  Next: Porting Common Lisp,  Prev: Common Lisp Compatibility,  Up: Top
  499. Old CL Compatibility
  500. ********************
  501. Following is a list of all known incompatibilities between this package
  502. and the older Quiroz `cl.el' package.
  503.    This package's emulation of multiple return values in functions is
  504. incompatible with that of the older package.  That package attempted to
  505. come as close as possible to true Common Lisp multiple return values;
  506. unfortunately, it could not be 100% reliable and so was prone to
  507. occasional surprises if used freely.  This package uses a simpler
  508. method, namely replacing multiple values with lists of values, which is
  509. more predictable though more noticeably different from Common Lisp.
  510.    The `defkeyword' form and `keywordp' function are not implemented in
  511. this package.
  512.    The `member', `floor', `ceiling', `truncate', `round', `mod', and
  513. `rem' functions are suffixed by `*' in this package to avoid collision
  514. with existing functions in Emacs 18 or Emacs 19.  The older package
  515. simply redefined these functions, overwriting the built-in meanings and
  516. causing serious portability problems with Emacs 19.  (Some more recent
  517. versions of the Quiroz package changed the names to `cl-member', etc.;
  518. this package defines the latter names as aliases for `member*', etc.)
  519.    Certain functions in the old package which were buggy or inconsistent
  520. with the Common Lisp standard are incompatible with the conforming
  521. versions in this package.  For example, `eql' and `member' were
  522. synonyms for `eq' and `memq' in that package, `setf' failed to preserve
  523. correct order of evaluation of its arguments, etc.
  524.    Finally, unlike the older package, this package is careful to prefix
  525. all of its internal names with `cl-'.  Except for a few functions which
  526. are explicitly defined as additional features (such as `floatp-safe'
  527. and `letf'), this package does not export any non-`cl-' symbols which
  528. are not also part of Common Lisp.
  529. The `cl-compat' package
  530. =======================
  531. The "CL" package includes emulations of some features of the old
  532. `cl.el', in the form of a compatibility package `cl-compat'.  To use
  533. it, put `(require 'cl-compat)' in your program.
  534.    The old package defined a number of internal routines without `cl-'
  535. prefixes or other annotations.  Call to these routines may have crept
  536. into existing Lisp code.  `cl-compat' provides emulations of the
  537. following internal routines: `pair-with-newsyms', `zip-lists',
  538. `unzip-lists', `reassemble-arglists', `duplicate-symbols-p',
  539. `safe-idiv'.
  540.    Some `setf' forms translated into calls to internal functions that
  541. user code might call directly.  The functions `setnth', `setnthcdr',
  542. and `setelt' fall in this category; they are defined by `cl-compat',
  543. but the best fix is to change to use `setf' properly.
  544.    The `cl-compat' file defines the keyword functions `keywordp',
  545. `keyword-of', and `defkeyword', which are not defined by the new "CL"
  546. package because the use of keywords as data is discouraged.
  547.    The `build-klist' mechanism for parsing keyword arguments is
  548. emulated by `cl-compat'; the `with-keyword-args' macro is not, however,
  549. and in any case it's best to change to use the more natural keyword
  550. argument processing offered by `defun*'.
  551.    Multiple return values are treated differently by the two Common
  552. Lisp packages.  The old package's method was more compatible with true
  553. Common Lisp, though it used heuristics that caused it to report
  554. spurious multiple return values in certain cases.  The `cl-compat'
  555. package defines a set of multiple-value macros that are compatible with
  556. the old CL package; again, they are heuristic in nature, but they are
  557. guaranteed to work in any case where the old package's macros worked.
  558. To avoid name collision with the "official" multiple-value facilities,
  559. the ones in `cl-compat' have capitalized names:  `Values',
  560. `Values-list', `Multiple-value-bind', etc.
  561.    The functions `cl-floor', `cl-ceiling', `cl-truncate', and
  562. `cl-round' are defined by `cl-compat' to use the old-style
  563. multiple-value mechanism, just as they did in the old package.  The
  564. newer `floor*' and friends return their two results in a list rather
  565. than as multiple values.  Note that older versions of the old package
  566. used the unadorned names `floor', `ceiling', etc.; `cl-compat' cannot
  567. use these names because they conflict with Emacs 19 built-ins.
  568. File: cl,  Node: Porting Common Lisp,  Next: Function Index,  Prev: Old CL Compatibility,  Up: Top
  569. Porting Common Lisp
  570. *******************
  571. This package is meant to be used as an extension to Emacs Lisp, not as
  572. an Emacs implementation of true Common Lisp.  Some of the remaining
  573. differences between Emacs Lisp and Common Lisp make it difficult to
  574. port large Common Lisp applications to Emacs.  For one, some of the
  575. features in this package are not fully compliant with ANSI or Steele;
  576. *note Common Lisp Compatibility::..  But there are also quite a few
  577. features that this package does not provide at all.  Here are some
  578. major omissions that you will want watch out for when bringing Common
  579. Lisp code into Emacs.
  580.    * Case-insensitivity.  Symbols in Common Lisp are case-insensitive
  581.      by default.  Some programs refer to a function or variable as
  582.      `foo' in one place and `Foo' or `FOO' in another.  Emacs Lisp will
  583.      treat these as three distinct symbols.
  584.      Some Common Lisp code is written in all upper-case.  While Emacs
  585.      is happy to let the program's own functions and variables use this
  586.      convention, calls to Lisp builtins like `if' and `defun' will have
  587.      to be changed to lower-case.
  588.    * Lexical scoping.  In Common Lisp, function arguments and `let'
  589.      bindings apply only to references physically within their bodies
  590.      (or within macro expansions in their bodies).  Emacs Lisp, by
  591.      contrast, uses "dynamic scoping" wherein a binding to a variable
  592.      is visible even inside functions called from the body.
  593.      Variables in Common Lisp can be made dynamically scoped by
  594.      declaring them `special' or using `defvar'.  In Emacs Lisp it is
  595.      as if all variables were declared `special'.
  596.      Often you can use code that was written for lexical scoping even
  597.      in a dynamically scoped Lisp, but not always.  Here is an example
  598.      of a Common Lisp code fragment that would fail in Emacs Lisp:
  599.           (defun map-odd-elements (func list)
  600.             (loop for x in list
  601.                   for flag = t then (not flag)
  602.                   collect (if flag x (funcall func x))))
  603.           
  604.           (defun add-odd-elements (list x)
  605.             (map-odd-elements (function (lambda (a) (+ a x))) list))
  606.      In Common Lisp, the two functions' usages of `x' are completely
  607.      independent.  In Emacs Lisp, the binding to `x' made by
  608.      `add-odd-elements' will have been hidden by the binding in
  609.      `map-odd-elements' by the time the `(+ a x)' function is called.
  610.      (This package avoids such problems in its own mapping functions by
  611.      using names like `cl-x' instead of `x' internally; as long as you
  612.      don't use the `cl-' prefix for your own variables no collision can
  613.      occur.)
  614.      *Note Lexical Bindings::, for a description of the `lexical-let'
  615.      form which establishes a Common Lisp-style lexical binding, and
  616.      some examples of how it differs from Emacs' regular `let'.
  617.    * Common Lisp allows the shorthand `#'x' to stand for `(function
  618.      x)', just as `'x' stands for `(quote x)'.  In Common Lisp, one
  619.      traditionally uses `#'' notation when referring to the name of a
  620.      function.  In Emacs Lisp, it works just as well to use a regular
  621.      quote:
  622.           (loop for x in y by #'cddr collect (mapcar #'plusp x))  ; Common Lisp
  623.           (loop for x in y by 'cddr collect (mapcar 'plusp x))    ; Emacs Lisp
  624.      When `#'' introduces a `lambda' form, it is best to write out
  625.      `(function ...)' longhand in Emacs Lisp.  You can use a regular
  626.      quote, but then the byte-compiler won't know that the `lambda'
  627.      expression is code that can be compiled.
  628.           (mapcar #'(lambda (x) (* x 2)) list)            ; Common Lisp
  629.           (mapcar (function (lambda (x) (* x 2))) list)   ; Emacs Lisp
  630.      Lucid Emacs supports `#'' notation starting with version 19.8.
  631.    * The "backquote" feature uses a different syntax in Emacs Lisp.
  632.           (defmacro foo (v &rest body) `(let ((,v 0)) @,body))  ; Common Lisp
  633.           (defmacro foo (v &rest body) (` (let (((, v) 0)) (@, body)))  ; Emacs
  634.    * Reader macros.  Common Lisp includes a second type of macro that
  635.      works at the level of individual characters.  For example, Common
  636.      Lisp implements the quote notation by a reader macro called `'',
  637.      whereas Emacs Lisp's parser just treats quote as a special case.
  638.      Some Lisp packages use reader macros to create special syntaxes
  639.      for themselves, which the Emacs parser is incapable of reading.
  640.      The lack of reader macros, incidentally, is the reason behind
  641.      Emacs Lisp's unusual backquote syntax.  Since backquotes are
  642.      implemented as a Lisp package and not built-in to the Emacs
  643.      parser, they are forced to use a regular macro named ``' which is
  644.      used with the standard function/macro call notation.
  645.    * Other syntactic features.  Common Lisp provides a number of
  646.      notations beginning with `#' that the Emacs Lisp parser won't
  647.      understand.  For example, `#| ... |#' is an alternate comment
  648.      notation, and `#+lucid (foo)' tells the parser to ignore the
  649.      `(foo)' except in Lucid Common Lisp.
  650.    * Packages.  In Common Lisp, symbols are divided into "packages".
  651.      Symbols that are Lisp built-ins are typically stored in one
  652.      package; symbols that are vendor extensions are put in another,
  653.      and each application program would have a package for its own
  654.      symbols.  Certain symbols are "exported" by a package and others
  655.      are internal; certain packages "use" or import the exported symbols
  656.      of other packages.  To access symbols that would not normally be
  657.      visible due to this importing and exporting, Common Lisp provides
  658.      a syntax like `package:symbol' or `package::symbol'.
  659.      Emacs Lisp has a single namespace for all interned symbols, and
  660.      then uses a naming convention of putting a prefix like `cl-' in
  661.      front of the name.  Some Emacs packages adopt the Common Lisp-like
  662.      convention of using `cl:' or `cl::' as the prefix.  However, the
  663.      Emacs parser does not understand colons and just treats them as
  664.      part of the symbol name.  Thus, while `mapcar' and `lisp:mapcar'
  665.      may refer to the same symbol in Common Lisp, they are totally
  666.      distinct in Emacs Lisp.  Common Lisp programs which refer to a
  667.      symbol by the full name sometimes and the short name other times
  668.      will not port cleanly to Emacs.
  669.      Emacs Lisp does have a concept of "obarrays," which are
  670.      package-like collections of symbols, but this feature is not
  671.      strong enough to be used as a true package mechanism.
  672.    * Keywords.  The notation `:test-not' in Common Lisp really is a
  673.      shorthand for `keyword:test-not'; keywords are just symbols in a
  674.      built-in `keyword' package with the special property that all its
  675.      symbols are automatically self-evaluating.  Common Lisp programs
  676.      often use keywords liberally to avoid having to use quotes.
  677.      In Emacs Lisp a keyword is just a symbol whose name begins with a
  678.      colon; since the Emacs parser does not treat them specially, they
  679.      have to be explicitly made self-evaluating by a statement like
  680.      `(setq :test-not ':test-not)'.  This package arranges to execute
  681.      such a statement whenever `defun*' or some other form sees a
  682.      keyword being used as an argument.  Common Lisp code that assumes
  683.      that a symbol `:mumble' will be self-evaluating even though it was
  684.      never introduced by a `defun*' will have to be fixed.
  685.    * The `format' function is quite different between Common Lisp and
  686.      Emacs Lisp.  It takes an additional "destination" argument before
  687.      the format string.  A destination of `nil' means to format to a
  688.      string as in Emacs Lisp; a destination of `t' means to write to
  689.      the terminal (similar to `message' in Emacs).  Also, format
  690.      control strings are utterly different; `~' is used instead of `%'
  691.      to introduce format codes, and the set of available codes is much
  692.      richer.  There are no notations like `\n' for string literals;
  693.      instead, `format' is used with the "newline" format code, `~%'.
  694.      More advanced formatting codes provide such features as paragraph
  695.      filling, case conversion, and even loops and conditionals.
  696.      While it would have been possible to implement most of Common Lisp
  697.      `format' in this package (under the name `format*', of course), it
  698.      was not deemed worthwhile.  It would have required a huge amount
  699.      of code to implement even a decent subset of `format*', yet the
  700.      functionality it would provide over Emacs Lisp's `format' would
  701.      rarely be useful.
  702.    * Vector constants use square brackets in Emacs Lisp, but `#(a b c)'
  703.      notation in Common Lisp.  To further complicate matters, Emacs 19
  704.      introduces its own `#(' notation for something entirely
  705.      different--strings with properties.
  706.    * Characters are distinct from integers in Common Lisp.  The
  707.      notation for character constants is also different:  `#\A' instead
  708.      of `?A'.  Also, `string=' and `string-equal' are synonyms in Emacs
  709.      Lisp whereas the latter is case-insensitive in Common Lisp.
  710.    * Data types.  Some Common Lisp data types do not exist in Emacs
  711.      Lisp.  Rational numbers and complex numbers are not present, nor
  712.      are large integers (all integers are "fixnums").  All arrays are
  713.      one-dimensional.  There are no readtables or pathnames; streams
  714.      are a set of existing data types rather than a new data type of
  715.      their own.  Hash tables, random-states, structures, and packages
  716.      (obarrays) are built from Lisp vectors or lists rather than being
  717.      distinct types.
  718.    * The Common Lisp Object System (CLOS) is not implemented, nor is
  719.      the Common Lisp Condition System.
  720.    * Common Lisp features that are completely redundant with Emacs Lisp
  721.      features of a different name generally have not been implemented.
  722.      For example, Common Lisp writes `defconstant' where Emacs Lisp
  723.      uses `defconst'.  Similarly, `make-list' takes its arguments in
  724.      different ways in the two Lisps but does exactly the same thing,
  725.      so this package has not bothered to implement a Common Lisp-style
  726.      `make-list'.
  727.    * A few more notable Common Lisp features not included in this
  728.      package:  `compiler-let', `tagbody', `prog', `ldb/dpb',
  729.      `parse-integer', `cerror'.
  730.    * Recursion.  While recursion works in Emacs Lisp just like it does
  731.      in Common Lisp, various details of the Emacs Lisp system and
  732.      compiler make recursion much less efficient than it is in most
  733.      Lisps.  Some schools of thought prefer to use recursion in Lisp
  734.      over other techniques; they would sum a list of numbers using
  735.      something like
  736.           (defun sum-list (list)
  737.             (if list
  738.                 (+ (car list) (sum-list (cdr list)))
  739.               0))
  740.      where a more iteratively-minded programmer might write one of
  741.      these forms:
  742.           (let ((total 0)) (dolist (x my-list) (incf total x)) total)
  743.           (loop for x in my-list sum x)
  744.      While this would be mainly a stylistic choice in most Common Lisps,
  745.      in Emacs Lisp you should be aware that the iterative forms are
  746.      much faster than recursion.  Also, Lisp programmers will want to
  747.      note that the current Emacs Lisp compiler does not optimize tail
  748.      recursion.
  749.